#include "queue.h"

static uint8_t m_UartBuf[100];
static queue_t m_UartQueue;

void init_status()
{
	create_queue(&m_UartQueue, m_UartBuf, sizeof(m_UartBuf));
}

void init_uart()
{
	GPIO_InitTypeDef GPIO_InitStructure;
	USART_InitTypeDef USART_InitStructure;
	
	RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1,ENABLE);
	
	/*
	*  USART1_TX -> PA9 , USART1_RX -> PA10
	*/				
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;	         
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; 
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  
	GPIO_Init(GPIOA, &GPIO_InitStructure);		   

	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;	        
	GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;  
	GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 
	GPIO_Init(GPIOA, &GPIO_InitStructure);
	
	USART_InitStructure.USART_BaudRate = 115200;
	USART_InitStructure.USART_WordLength = USART_WordLength_8b;
	USART_InitStructure.USART_StopBits = USART_StopBits_1;
	USART_InitStructure.USART_Parity = USART_Parity_No;
	USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
	USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
	
	USART_Init(USART1, &USART_InitStructure); 
	USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
	USART_Cmd(USART1, ENABLE);
}

void NVIC_Configuration(void)
{
	NVIC_InitTypeDef NVIC_InitStructure;

	/* Configure the NVIC Preemption Priority Bits */  
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);

	/* Enable the USART1 Interrupt */
	NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
	NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
	NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStructure);
}

}

int uart_send_data(uint8_t *pData, int len)
{
	int i = 0;
	for(i = 0; i < len; i++)
	{
		USART_SendData(UART1, pData[i]);

		/* Loop until the end of transmission */
		while (USART_GetFlagStatus(UART1, USART_FLAG_TC) == RESET)
		{}
	}
	
	return i;
}

void init_sim5320()
{
	init_status();
	init_uart();
}

int sim5320_core_process(uint8_t *pCmd, uint8_t *pResponse)
{
	
}

int sim5320_send_message(uint8_t *pPhoneNumber, uint8_t *pText)
{
	if(strlen(pPhoneNumber) != 11)
		return -1;
}

void USART1_IRQHandler(void)
{
	if(USART_GetITStatus(UART1, USART_IT_RXNE) != RESET)
	{
		uint8_t byte = USART_ReceiveData(USART1);
		add_queue_data(&m_UartQueue, &byte, 1);
	}


